package com.pdg.android.splorr.states;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.pdg.android.common.ParentedBase;
import com.pdg.android.common.State;
import com.pdg.android.splorr.R;
import com.pdg.android.splorr.SplorrActivity;
import com.pdg.android.splorr.data.Game;
import com.pdg.android.splorr.data.Player;
import com.pdg.android.splorr.data.PlayerItem;
import com.pdg.android.splorr.data.enums.GameState;

public class PlayerInventoryState  extends
State<Game, SplorrActivity, GameState>{
	
	private class DropActionClickListener extends ParentedBase<PlayerInventoryState>  implements OnClickListener{
		
		private int itemIndex;

		public DropActionClickListener(PlayerInventoryState theParent,int theItemIndex) {
			super(theParent);
			setItemIndex(theItemIndex);
		}

		public void onClick(View v) {
			getParent().dropItem(getItemIndex());
		}

		private int getItemIndex() {
			return itemIndex;
		}

		private void setItemIndex(int itemIndex) {
			this.itemIndex = itemIndex;
		}
		
	}
	
	private class EquipActionClickListener extends ParentedBase<PlayerInventoryState>  implements OnClickListener{
		
		private int itemIndex;

		public EquipActionClickListener(PlayerInventoryState theParent,int theItemIndex) {
			super(theParent);
			setItemIndex(theItemIndex);
		}

		public void onClick(View v) {
			getParent().equipItem(getItemIndex());
		}

		private int getItemIndex() {
			return itemIndex;
		}

		private void setItemIndex(int itemIndex) {
			this.itemIndex = itemIndex;
		}
	}

	private class UnequipActionClickListener extends ParentedBase<PlayerInventoryState>  implements OnClickListener{
		
		private int itemIndex;

		public UnequipActionClickListener(PlayerInventoryState theParent,int theItemIndex) {
			super(theParent);
			setItemIndex(theItemIndex);
		}

		public void onClick(View v) {
			getParent().unequipItem(getItemIndex());
		}

		private int getItemIndex() {
			return itemIndex;
		}

		private void setItemIndex(int itemIndex) {
			this.itemIndex = itemIndex;
		}
	}

	public PlayerInventoryState(Game theData, SplorrActivity theMachine) {
		super(theData, theMachine, GameState.PLAYER_INVENTORY);
	}

	public void unequipItem(int itemIndex) {
		// TODO Auto-generated method stub
		
	}

	public void equipItem(int itemIndex) {
		// TODO Auto-generated method stub
		
	}

	public void dropItem(int theItemIndex) {
		getData().dropItem(theItemIndex);
		refreshDisplay();
	}

	@Override
	protected void onInitialize() {
	}

	@Override
	protected void onEnter() {
		getMachine().setContentView(R.layout.room_items);
		refreshDisplay();
	}

	private void refreshDisplay() {
		Player thePlayer = getData().getPlayerData();
		TextView tv = (TextView)getMachine().findViewById(R.id.title);
		tv.setText(getData().getCurrentEquipmentSummary());
		
		TableLayout tl = (TableLayout)getMachine().findViewById(R.id.table);
		
		tl.removeAllViews();
		
		for(int theIndex=0;theIndex<thePlayer.getItemCount();++theIndex){
			PlayerItem theItem = thePlayer.getItems().get(theIndex);
			TableRow tr = new TableRow(getMachine());
			tv = new TextView(getMachine());
			tv.setText(theItem.getName());
			
			tr.addView(tv);
			
			Button btn = new Button(getMachine());
			
			btn.setText(getMachine().getString(R.string.button_drop));
			btn.setOnClickListener(new DropActionClickListener(this,theIndex));
			
			tr.addView(btn);
			
			if(theItem.isEquipped()){
				btn = new Button(getMachine());
				
				btn.setText(getMachine().getString(R.string.button_unequip));
				btn.setOnClickListener(new UnequipActionClickListener(this,theIndex));
				
				tr.addView(btn);
				
			} else if(theItem.isEquippable()){
				btn = new Button(getMachine());
				
				btn.setText(getMachine().getString(R.string.button_equip));
				btn.setOnClickListener(new EquipActionClickListener(this,theIndex));
				
				tr.addView(btn);
				
			}
			
			tl.addView(tr);
			
		}
	}

	@Override
	protected void onLeave() {
	}

	@Override
	protected void onCleanUp() {
	}

	@Override
	protected void onButtonPressed(int theButtonId) {
		getMachine().setCurrent(GameState.ROOM_SUMMARY);
	}

}
